<?php

defined('_JEXEC') or die;
require_once (JPATH_COMPONENT.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'utils.php');
class BakerysModelOrder extends JModelAdmin
{

	protected $text_prefix = 'COM_BAKERYS';

	protected function canDelete($record)
	{
		if (!empty($record->id))
		{
			if ($record->state != -2)
			{
				return;
			}
			return parent::canDelete($record);
		}
	}

	public function getTable($type = 'order', $prefix = 'BakerysTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	public function getForm($data = array(), $loadData = true)
	{
		$app = JFactory::getApplication();

		// Get the form.
		$form = $this->loadForm('com_bakerys.item', 'item', array('control' => 'jform', 'load_data' => $loadData));
		
		return $form;
	}

	
	public function getItem($pk = null)
	{				
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);
		$id = JRequest::getVar("id");			
		$query->select("o.*");
		$query->from("#__bakery_orders o");
		$query->where('o.id = '.(int) $id);
		$db->setQuery($query);			
		return $db->loadObject();		
	}	
	
	function delivery($pks)
	{
		$db = JFactory::getDbo();
		$query = "update #__bakery_orders set status = 1 where id in (".implode(',',$pks).")";
		$db->setQuery($query);
		$db->query();
	}
	
	function undelivered($pks)
	{
		$db = JFactory::getDbo();
		$query = "update #__bakery_orders set status = 0 where id in (".implode(',',$pks).")";
		$db->setQuery($query);
		$db->query();
	}
}
